package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OrderRenegeMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OrderRenegeCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderRenegeCostModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderRenegePayeeModifyDto;
import com.molichuxing.services.infrastructure.dto.response.OrderNegotiateRenegeDto;
import com.molichuxing.services.infrastructure.dto.response.OrderRenegeDto;
import com.molichuxing.services.infrastructure.service.OrderRenegeService;
import com.molichuxing.services.property.OrderRenegeStatusEnum;
import com.molichuxing.services.property.OrderRenegeTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 订单退租service
 *
 * @author zoumingyu
 * @date 2020年2月26日
 */
@Service("orderRenegeService")
public class OrderRenegeServiceImpl implements OrderRenegeService {
    private static final Logger logger = LoggerFactory.getLogger(OrderRenegeServiceImpl.class);

    @Autowired
    private OrderRenegeMapper orderRenegeMapper;

    /**
     * 订单退租新增
     *
     * @param createDto
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean create(OrderRenegeCreateDto createDto) {
        return orderRenegeMapper.create(Convert.toOrderRenegeBean(createDto)) > 0 ? true : false;
    }

    /**
     * 查询退租信息
     *
     * @param orderCode    订单号
     * @param renegeStatus 退租状态
     * @return
     */
    @Override
    public OrderRenegeDto getRenege(Long orderCode, OrderRenegeStatusEnum renegeStatus) {
        return Convert.toOrderRenegeDto(
                orderRenegeMapper.getRenege(orderCode, renegeStatus == null ? null : renegeStatus.getValue()));
    }

    /**
     * 查询有效退租信息
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    public OrderRenegeDto getEffectiveRenege(Long orderCode) {
        List<Integer> effectiveRenegeStatus = Arrays.asList(OrderRenegeStatusEnum.RENEGE.getValue(),
                OrderRenegeStatusEnum.COMPLETE.getValue());
        return Convert.toOrderRenegeDto(
                orderRenegeMapper.getRenegeByRenegeStatus(orderCode, effectiveRenegeStatus));
    }

    /**
     * 查询有效退租信息
     *
     * @param renegeTimeStart 退租时间-开始
     * @param renegeTimeEnd   退租时间-结束
     * @return
     */
    @Override
    public List<OrderRenegeDto> getEffectiveRenegeList(LocalDateTime renegeTimeStart, LocalDateTime renegeTimeEnd) {
        List<Integer> effectiveRenegeStatus = Arrays.asList(OrderRenegeStatusEnum.RENEGE.getValue(),
                OrderRenegeStatusEnum.COMPLETE.getValue());
        return Convert.toOrderRenegeDto(
                orderRenegeMapper.getRenegeByRenegeTime(renegeTimeStart, renegeTimeEnd, effectiveRenegeStatus));
    }

    /**
     * 订单退租取消
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean modifyCancel(Long orderCode) {
        return orderRenegeMapper.modifyRenegeStatus(orderCode, OrderRenegeStatusEnum.CANCEL.getValue(),
                OrderRenegeStatusEnum.RENEGE.getValue()) > 0 ? true : false;
    }

    /**
     * 订单退租费用修改
     *
     * @param modifyDto
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean modifyCost(OrderRenegeCostModifyDto modifyDto) {
        return orderRenegeMapper.modifyCost(Convert.toOrderRenegeBean(modifyDto)) > 0 ? true : false;
    }

    /**
     * 订单退租费用修改
     *
     * @param modifyDto
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean modifyPayee(OrderRenegePayeeModifyDto modifyDto) {
        return orderRenegeMapper.modifyPayee(Convert.toOrderRenegeBean(modifyDto)) > 0 ? true : false;
    }

    /**
     * 订单退租完成
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean modifyComplete(Long orderCode) {
        return orderRenegeMapper.modifyRenegeStatus(orderCode, OrderRenegeStatusEnum.COMPLETE.getValue(),
                OrderRenegeStatusEnum.RENEGE.getValue()) > 0 ? true : false;
    }

    /**
     * 退租总数
     *
     * @param renegeType 退租类型
     * @return
     */
    @Override
    public int getRenegeTotal(OrderRenegeTypeEnum renegeType) {
        return (int) orderRenegeMapper.getRenegeTotal(renegeType.getValue());
    }

    /**
     * 查询协商退租列表
     *
     * @param params   查询参数
     * @param pageNum  页数
     * @param pageSize 每页条数
     * @return Paged
     */
    @Override
    public Paged<OrderNegotiateRenegeDto> getNegotiateRenegePaged(Map<String, Object> params,
                                                                  int pageNum, int pageSize) {

        Paged<OrderNegotiateRenegeDto> result = new Paged<>(null, 0, pageSize);
        // 退租开始时间-LocalDateTime
        params.put("renegeTimeStart", ObjectCastUtil.castLocalDateTimeStart(params.get("lawsuitTimeStart")));
        // 退租结束时间-LocalDateTime
        params.put("renegeTimeEnd", ObjectCastUtil.castLocalDateTimeEnd(params.get("lawsuitTimeEnd")));

        result.setTotal(orderRenegeMapper.getPagedCount(params));
        if (result.getTotal() > 0) {
            result.setList(orderRenegeMapper.getPaged(params, pageNum, pageSize));
        }

        return result;
    }

    /**
     * 订单诉讼
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    public Boolean modifyLawsuit(Long orderCode) {
        return orderRenegeMapper.modifyRenegeStatus(orderCode, OrderRenegeStatusEnum.LAWSUIT.getValue(),
                OrderRenegeStatusEnum.RENEGE.getValue()) > 0;
    }

    @Override
    public Boolean modifyOverdue(Long orderCode, Integer overdueDaysTotal, Integer overdueNumTotal) {
        return orderRenegeMapper.modifyOverdue(orderCode, OrderRenegeStatusEnum.RENEGE.getValue(),
                overdueDaysTotal, overdueNumTotal) > 0;
    }


}
